// 兑换爱币
import { client } from '../../../request/index.js'
import Toast from '../../../miniprogram_npm/@vant/weapp/toast/toast'
Page({
  data: {
    imgIp: getApp().globalData.imgIp,
    gCoin: 0,
    aCoin: 0,
    bankCard: '无',
    inputV: '',
    flag: false,
  },

  toinPut1: function () {
    this.setData({
      inputV: 1,
    })
  },

  toinPut2: function () {
    this.setData({
      inputV: 5,
    })
  },

  toinPut3: function () {
    this.setData({
      inputV: 10,
    })
  },
  //用户信息
  async userInformation() {
    const sessId = getApp().globalData.sessionid
    const getData = await client({
      url: '/account/detail',
    })
    console.log('用户信息', getData)
    wx.setStorageSync('gCoin', getData.data.data.gCoin)
    wx.setStorageSync('aCoin', getData.data.data.aCoin)
    console.log(wx.getStorageSync('gCoin'))
    console.log(wx.getStorageSync('aCoin'))

    this.setData({
      aCoin: wx.getStorageSync('aCoin'),
      gCoin: wx.getStorageSync('gCoin'),
      userId: getData.data.data.userId,
      inputV: '',
      flag: true,
    })
  },
  //光币爱币转换
  async acoinExchangeAcoin(e) {
    const sessId = getApp().globalData.sessionid
    const getData = await client({
      url: '/account/exchange?gCoin=' + e.detail.value.gCoin,
      method: 'POST',
      data: {
        gCoin: e.detail.value.gCoin,
      },
    })
    console.log('光币爱币转换', getData)
  },
  //提交表单
  submitCoin: function (e) {
    let that = this
    if (e.detail.value.gCoin == '') {
      wx.showModal({
        // 提示框
        title: '提示',
        content: '请输入光币',
        showCancel: false,
        confirmColor: '#ff6b69',
        confirmText: '返回输入',
      })
      return
    }
    if (parseInt(e.detail.value.gCoin) > parseInt(this.data.gCoin)) {
      Toast.fail('光币余额不足')
      this.setData({
        inputV: '',
      })
      return
    } else {
      this.userInformation()
      if (this.data.flag) {
        this.acoinExchangeAcoin(e)
        Toast.success('兑换成功')
        this.userInformation()
      } else {
        Toast.fail('兑换失败')
      }
    }
  },

  onLoad: function (options) {
    this.userInformation()
  },

  onPullDownRefresh: function () {
    this.setData({
      inputV: '',
    })
    this.userInformation()
    setTimeout(() => {
      wx.stopPullDownRefresh()
    }, 1000)
  },
})
